<?php

	/*
	*	file: pageStructUIMod.php
	*
	*	contents:	provides functions to show contents like insert form, search form...
	*
	*	author: Lisa Pedrazzi, Federico Zanco
	*/



	/*
	* function showResult($res, $refineButton)
	*
	* Input required:	$res: a valid query result
	*					$refineButton: flag to show refine button on form
	*
	* Output:	return html code to build a table with result values
	*
	* Author: Federico Zanco
	*/

	function showResult($res, $refineButton) {
		global $con,$docFieldsName, $docFieldsNameLocale
;
		$text = "
				<table>
					<thead st>";
	
		//head of the table
		for ($i = 1; $i < mysql_num_fields($res); $i++) {
			$text = $text . "
						<th class=\"rescellhead\">" . $docFieldsNameLocale[array_search(mysql_fetch_field($res, $i)->name, $docFieldsName)] . "</th>";
		}
	
			$text = $text . "
					</thead>";
		
		//rows with values
		while ($row = mysql_fetch_array($res)) {
			$text = $text . "
					<tr>";

			//if value's length is bigger than 50 char cut it
			for ($i = 1; $i < mysql_num_fields($res); $i++) {
				if (strlen($row[$i]) > 50)
					$row[$i] = substr($row[$i], 0, 50) . "(...)";
					
				//if the value is a date then convert it from mysql to "normal" format
				if (mysql_fetch_field($res, $i)->type == 'date')
					$text = $text . "
						<td class=\"rescell\"><div class=\"rescellcontent\">" . mysql2NormalDate($row[$i]) . "</div></td>";
				else
					$text = $text . "
						<td class=\"rescell\"><div class=\"rescellcontent\">" . $row[$i] . "</div></td>";
			}

			//make the entry key
			$key = "doc_num=" . $row[0] . "&from=" . $row[2] . "&to=" . $row[3];
		
			//insert Show icon. The link send the key via GET
			$text = $text . "
						<td class=\"rescell\"><a href=\"show.php?$key\"><img src=\"img/style/info.png\" style=\"border: 0px;\" title=\"Visualizza\"></a></td>";

			//if user has edit authorization then show Edit icon
			if ($_SESSION['edit_auth'])
				$text = $text . "
						<td class=\"rescell\"><a href=\"edit.php?$key\"><img src=\"img/style/modify.gif\" style=\"border: 0px;\" title=\"Modifica\"></a></td>";


			//if user has search authorization then show Download icon
			if ($_SESSION['search_auth'])
				$text = $text . "
						<td class=\"rescell\"><a href=\"download.php?$key\"><img src=\"img/style/folder_outbox.png\" style=\"border: 0px;\" title=\"Scarica\"></a></td>";
			
			$text = $text . "
					</tr>";
		}

		//if set $refineButton add refine button
		if ($refineButton) {
			$refineButtonHtml = "
						<input name=\"Raffina Ricerca\" type=\"submit\" value=\"Raffina Ricerca\" />";
		} else
			$refineButtonHtml = "";

		//if user has search authorization then show New Search button
		if ($_SESSION['search_auth'])
			$newSearchButtonHtml = "
				<div>
					<form action=\"search.php\" method=\"post\" name=\"newSearch\">
						<input name=\"Nuova Ricerca\" type=\"submit\" value=\"Nuova Ricerca\" />
					</form>
				</div>";
			
		$text = $text . "
				</table>";

		//if no results to show then notify it
		if (mysql_affected_rows($con) == 0)
			$text = "<br /><h3>La ricerca non ha prodotto risultati</h3><br />";
	
		$text = $text . "
	
				<div style=\"float:left\">
					<form action=\"search.php\" method=\"post\" name=\"betterSearch\">
						<input type=\"hidden\" name=\"created_by\" value=\"" . stripslashes($_POST['created_by']) . "\" />
						<input type=\"hidden\" name=\"inserted_by\" value=\"" . stripslashes($_POST['inserted_by']) . "\" />
						<input type=\"hidden\" name=\"from\" value=\"" . stripslashes($_POST['from']) . "\" />
						<input type=\"hidden\" name=\"to\" value=\"" . stripslashes($_POST['to']) . "\" />
						<input type=\"hidden\" name=\"from_date\" value=\"" . stripslashes($_POST['from_date']) . "\" />
						<input type=\"hidden\" name=\"to_date\" value=\"" . stripslashes($_POST['to_date']) . "\" />
						<input type=\"hidden\" name=\"expires\" value=\"" . stripslashes($_POST['expires']) . "\" />
						<input type=\"hidden\" name=\"type\" value=\"" . stripslashes($_POST['type']) . "\" />
						<input type=\"hidden\" name=\"department\" value=\"" . stripslashes($_POST['department']) . "\" />
						<input type=\"hidden\" name=\"description\" value=\"" . stripslashes($_POST['description']) . "\" />
						$refineButtonHtml
					</form>
				</div>

				$newSearchButtonHtml";

		return $text;
	}



	/*
	* function showEntry($res)
	*
	* Input required:	$con: a valid query result
	*
	* Output:	shows entry values of the given result.
	*
	* Author: Federico Zanco
	*/

	function showEntry($res) {
		//get result values
		$row = mysql_fetch_assoc($res);
		
		//build the entry key
		$key = "doc_num=" . $row['doc_num'] . "&from=" . $row['from'] . "&to=" . $row['to'];
		
		//if user has search authorization then show Download icon
		if ($_SESSION['search_auth'])
			$download = "<a href=\"download.php?$key\"><img src=\"img/style/folder_outbox.png\" style=\"border: 0px;\"></a>";

		return "
				<table>
					<thead>
						<th class=\"detcellhead\">
							Campo
						</th>
						<th class=\"detcellhead\">
							Valore
						</th>
					</thead>
					<tr>
						<td class=\"detcell\">Codice</td>
						<td class=\"detcell\">" . $row['doc_num'] . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Documento</td>
						<td class=\"detcell\">" . $row['filename'] . " $download</td>
					</tr>
					<tr>
						<td class=\"detcell\">Creato da</td>
						<td class=\"detcell\">" . $row['created_by'] . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Inserito da</td>
						<td class=\"detcell\">" . $row['inserted_by'] . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Proveniente da</td>
						<td class=\"detcell\">" . $row['from'] . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Diretto a</td>
						<td class=\"detcell\">" . $row['to'] . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Data documento</td>
						<td class=\"detcell\">" . mysql2NormalDate($row['date']) . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Validit&agrave;</td>
						<td class=\"detcell\">" . mysql2NormalDate($row['expires']) . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Tipologia di documento</td>
						<td class=\"detcell\">" . $row['type'] . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Settore di compentenza</td>
						<td class=\"detcell\">" . $row['department'] . "</td>
					</tr>
					<tr>
						<td class=\"detcell\">Descrizione</td>
						<td class=\"detcell\">" . $row['description'] . "</td>
					</tr>
				</table>
				<br />
				<a href=\"javascript:history.go(-1)\">Indietro</a>";
	}



	/*
	* function showOptions($field, $table, $default)
	*
	* Input required:	$field: field to show
	*					$table: field's table
	*					$default: default value to set
	*
	* Output:	return HTML code to fill a <select> tag
	*
	* Author: Federico Zanco
	*/

	function showOptions($field, $table, $default) {
		global $con;

		//exec a query to get field's values
		$res = query("SELECT `$field` FROM $table", $con);

		//if any...
		if (mysql_affected_rows($con) > 0) {
		
			$opts = "";
			$field = trim($field, "`");
		
			//get every value and build <options> tag
			while ($row = mysql_fetch_assoc($res)) {
				if ( $default == $row["$field"] )
					$opts = $opts . "
				<option value=\"" . stripslashes($row["$field"]) . "\" selected=\"selected\">" . stripslashes($row["$field"]) . "</option>";
				else
					$opts = $opts . "
				<option value=\"" . stripslashes($row["$field"]) . "\" >" . stripslashes($row["$field"]) . "</option>";
			}
		
			return $opts;
    	}

		//return empty string if query didn't return any value
    	return "";
	}



	/*
	* function showFormDocInsert()
	*
	* Input required:   -
	*
	* Output: shows to screen a piece of xhtml, the table for inserting a document
	*
	* Author: Lisa Pedrazzi, Zanco Federico
	*/
	
	function showFormDocInsert() {
		//print_r($_POST);

		/*Checking for preinserted values*/

		//document number
		if (isset($_POST['doc_num']))
			$docNum = $_POST['doc_num'];
		else
			$docNum = "";
		
		//created by
		if (isset($_POST['created_by']))
			$created_by = $_POST['created_by'];
		else
			$created_by = "";
		
		//from
		if (isset($_POST['from']))
			$from = $_POST['from'];
		else
			$from = "";
    	
		//to
		if (isset($_POST['to']))
			$to = $_POST['to'];
		else
			$to = "";
	
		//date
		if (isset($_POST['date']))
			$date = $_POST['date'];
		else {
			$tmp = getdate();
			$date = $tmp[mday]."/".$tmp[mon]."/".$tmp[year];
		 }
		 
		 //expires
		if (isset($_POST['expires']))
			$expires = $_POST['expires'];
		else
			$expires = "";
	 	
		//type
		if (isset($_POST['type']))
		    $type = $_POST['type'];
		else
		    $type = "";

		//id logged user's department is Amministrazione then add department control to form
		if ($_SESSION['department'] == "Amministrazione") { 
			//department
			if (isset($_POST['department']))
				$department = $_POST['department'];
			else
				$department = "";
				
			$departmentHtml = "
						<tr>
							<td><label for=\"department\">Settore di competenza: </label></td>
							<td>
								<select name=\"department\" id=\"department\">
									<option value =\"\"></option>
									" . showOptions('name', 'departments', $department) . "
								</select>
							</td>
						</tr>";
		}

		//description
		if (isset($_POST['description']))
			$description = stripslashes($_POST['description']);
		else
			$description = "";

		return "
				<form action=\"insert.php\" method=\"POST\" enctype=\"multipart/form-data\" name=\"insertDocument\">
					<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"16000000\" />
					<table id =\"insertTable\">
						<tr>
							<td><label for=\"userfile\">Documento: </label></td>
							<td><input type=\"file\" name=\"userfile\" id=\"userfile\"/></td>
						</tr>
						<tr>
							<td><label for=\"created_by\">Creato da: </label></td>
							<td>
								<select name=\"created_by\" id=\"created_by\">
									<option value =\"\"> (nessuno)</option>
									" . showOptions('username', 'users', $created_by) . "
								</select>
							</td>
						</tr>
						<tr>
							<td><label for=\"from\">Proveniente da: </label></td>
							<td>
								<select name=\"from\" id=\"from\" onchange=\"javascript:checkSubjects(this);\">
									<option value =\"\"></option>
									" . showOptions('name', 'communication_subjects', $from) . "
								</select>
							</td>
							<td>
								<input type=\"button\" onclick=\"javascript:getSubjectName('from')\" value=\"Nuovo\" />
							</td>
						</tr>
						<tr>
							<td><label for=\"to\">Diretto a: </label></td>
							<td>
								<select name=\"to\" id=\"to\" onchange=\"javascript:checkSubjects(this);\">
									<option value =\"\"></option>
									" . showOptions('name', 'communication_subjects', $to) . "
								</select>
							</td>
							<td>
								<input type=\"button\" onclick=\"javascript:getSubjectName('to')\" value=\"Nuovo\" />
							</td>
						</tr>
						<tr>
							<td><label for=\"date\">Data documento: </label></td>
							<td><input type=\"text\" name=\"date\" id=\"date\" value=\"$date\" />
							" . selectDate("insertDocument", "date") . "</td>
						</tr>
						<tr>
							<td><label for=\"expires\">Validit&agrave;: </label></td>
							<td><input type=\"text\" name=\"expires\" id=\"expires\" value=\"$expires\" />
							" . selectDate("insertDocument", "expires") . "</td>
						</tr>
						<tr>
							<td><label for=\"type\">Tipologia di documento: </label></td>
							<td>
								<select name=\"type\" id=\"type\">
									<option value =\"\"></option>
									" . showOptions('type', 'document_types', $type) . "
								</select>
							</td>
						</tr>
						$departmentHtml
						<tr>
							<td><label for=\"description\">Descrizione: </td>
							<td><textarea rows=\"4\" cols=\"20\" name=\"description\" id=\"description\">$description</textarea></td>
						</tr>
					</table>
					<div style=\"float: left;\"><input name=\"Inserisci\" type=\"submit\" value=\"Inserisci\" /></div>
					<input name=\"Svuota campi\" type=\"reset\" value=\"Svuota campi\" />
				</form> ";
	}



	/*
	* function showFormDocEdit($res)
	*
	* Input required:	$res: a query result to preset values
	*
	* Output:	shows to screen a piece of xhtml, the table for editing a document
	*
	* Author: Federico Zanco
	*/

	function showFormDocEdit($res) {

		//get the query result values to preset form controls
		$row = mysql_fetch_assoc($res);
		
		//document number
		$docNum = $row['doc_num'];
		
		//filename
		$filename = $row['filename'];
		
		//created by
		if (isset($_GET['created_by']))
			$created_by = $_GET['created_by'];
		else
			$created_by = $row['created_by'];
		
		//inserted by
		$inserted_by = $row['inserted_by'];
		
		//from
		$from = $row['from'];
    	
		//to
		$to = $row['to'];
	
		//date
		$date = mysql2NormalDate($row['date']);
		 
		 //expires
		if (isset($_GET['expires']))
			$expires = $_GET['expires'];
		else {
			if ($row['expires'] == "0000-00-00")
				$expires = "";
			else
				$expires = mysql2NormalDate($row['expires']);
		}
	 
		//type
		if (isset($_GET['type']))
		    $type = $_GET['type'];
		else
		    $type = $row['type'];

		//id logged user's department is Amministrazione then add department control to form
		if ($_SESSION['department'] == "Amministrazione") {
			//department
			if (isset($_GET['department']))
				$department = $_GET['department'];
			else
				$department = $row['department'];
				
			$departmentHtml = "
						<tr>
							<td><label for=\"department\">Settore di competenza: </label></td>
							<td>
								<select name=\"department\" id=\"department\">
									<option value =\"\"></option>
									" . showOptions('name', 'departments', $department) . "
								</select>
							</td>
						</tr>";
		}

		//description
		if (isset($_GET['description']))
			$description = stripslashes($_GET['description']);
		else
			$description = $row['description'];
	
		return "
				<form action=\"edit.php\" method=\"GET\" name=\"editDocument\">
					<table id =\"editTable\">
						<tr>
							<td><label for=\"docNum\">Codice documento: </label></td>
							<td>$docNum</td>
						</tr>
						<tr>
							<td><label for=\"userfile\">Documento: </label></td>
							<td>$filename</td>
						</tr>
						<tr>
							<td><label for=\"created_by\">Creato da: </label></td>
							<td>
								<select name=\"created_by\" id=\"created_by\">
									<option value =\"\"> (nessuno)</option>
									" . showOptions('username', 'users', $created_by) . "
								</select>
							</td>
						</tr>
						<tr>
							<td><label for=\"inserted_by\">Inserito da: </td>
							<td>$inserted_by</td>
						</tr>
						<tr>
							<td><label for=\"from\">Proveniente da: </label></td>
							<td>$from</td>
						</tr>
						<tr>
							<td><label for=\"to\">Diretto a: </label></td>
							<td>$to</td>
						</tr>
						<tr>
							<td><label for=\"date\">Data documento: </label></td>
							<td>$date</td>
						</tr>
						<tr>
							<td><label for=\"expires\">Validit&agrave;: </label></td>
							<td><input type=\"text\" name=\"expires\" id=\"date\" value=\"$expires\" />
							" . selectDate("editDocument", "expires") . "</td>
						</tr>
						<tr>
							<td><label for=\"type\">Tipologia di documento: </label></td>
							<td>
								<select name=\"type\" id=\"type\">
									<option value =\"\"></option>
									" . showOptions('type', 'document_types', $type) . "
								</select>
							</td>
						</tr>
						$departmentHtml
						<tr>
							<td><label for=\"description\">Descrizione: </td>
							<td><textarea rows=\"4\" cols=\"20\" name=\"description\" id=\"description\">$description</textarea></td>
						</tr>						
					</table>

					<input name=\"doc_num\" type=\"hidden\" value=\"$docNum\" />					
					<input name=\"from\" type=\"hidden\" value=\"$from\" />
					<input name=\"to\" type=\"hidden\" value=\"$to\" />
					
					<div style=\"float: left;\"><input name=\"Modifica\" type=\"submit\" value=\"Modifica\" /></div>
					<input name=\"Svuota campi\" type=\"reset\" value=\"Ripristina campi\" />
				</form> ";
	}



	/*
	* function showFormDocSearch()
	*
	* Input required:	none
	*
	* Output: shows to screen a piece of xhtml, the table for searching a document
	*
	* Author: Lisa Pedrazzi, Federico Zanco
	*/
	
	function showFormDocSearch(){
		/*Checking for preinserted values*/
		
		//created by
		if (isset($_POST['created_by']))
			$created_by = stripslashes($_POST['created_by']);
		else
			$created_by = "";

		//inserted_by
		if (isset($_POST['inserted_by']))
			$inserted_by = stripslashes($_POST['inserted_by']);
		else
			$inserted_by = "";

		//from
		if (isset($_POST['from']))
			$from = stripslashes($_POST['from']);
		else
			$from = "";

		//to
		if (isset($_POST['to']))
		    $to = stripslashes($_POST['to']);
		else
		    $to = "";

		//from date
		if (isset($_POST['from_date']))
			$fromDate = stripslashes($_POST['from_date']);
		else
			$fromDate = "";

		//to date
		if (isset($_POST['to_date']))
			$toDate = stripslashes($_POST['to_date']);
		else {
			$tmp = getdate(); 
			$toDate = $tmp[mday]."/".$tmp[mon]."/".$tmp[year];
		}
		
		//to date
		if ($_POST['expires'] == "on")
			$expires = "checked=\"checked\"";
		else {
			$expires = "";
		}

		//id logged user's department is Amministrazione then add department control to form
		if ($_SESSION['department'] == "Amministrazione") { 
			//department
			if (isset($_POST['department']))
				$department = $_POST['department'];
			else
				$department = "";
				
			$departmentHtml = "
						<tr>
							<td><label for=\"department\">Settore di competenza: </label></td>
							<td>
								<select name=\"department\" id=\"department\">
									<option value =\"Tutti\" selected=\"selected\">Tutti</option>
									" . showOptions('name', 'departments', $department) . "
								</select>
							</td>
						</tr>";
		}

		//document type
		if (isset($_POST['type']))
			$type = stripslashes($_POST['type']);
		else
			$type = "";

		//description
		if (isset($_POST['description']))
			$description = stripslashes($_POST['description']);
		else
			$description = "";

		return "
				<form action=\"search.php\" method=\"post\" enctype=\"multipart/form-data\" name=\"searchDocument\">
					<table id =\"searchTable\">
						<!--<tr>
							<td><label for=\"doc_num\">Codice Documento: </label></td>
							<td><input type=\"text\" name=\"doc_num\" id=\"doc_num\" value=\"$doc_num\" /></td>
						</tr>-->
						<tr>
							<td><label for=\"created_by\">Creato da: </label></td>
							<td>
								<select name=\"created_by\" id=\"created_by\">
									<option value =\"Tutti\" selected=\"selected\">Tutti</option>
									" . showOptions("username", "users", $created_by) . "
								</select>
							</td>
						</tr>
						<tr>
							<td><label for=\"inserted_by\">Inserito da: </label></td>
							<td>
								<select name=\"inserted_by\" id=\"inserted_by\">
									<option value =\"Tutti\" selected=\"selected\">Tutti</option>
									" . showOptions("username", "users", $inserted_by) . "
								</select>
							</td>
						</tr>
						<tr>
							<td><label for=\"from\">Proveniente da: </label></td>
							<td>
								<select name=\"from\" id=\"from\">
									<option value =\"Tutti\">Tutti</option>
									" . showOptions("name", "communication_subjects", $from) . "
								</select>
							</td>
						</tr>
						<tr>
							<td><label for=\"to\">Diretto a: </label></td>
							<td>
								<select name=\"to\" id=\"to\">
									<option value =\"Tutti\" selected=\"selected\">Tutti</option>
									" . showOptions("name", "communication_subjects", $to) . "
								</select>
							</td>
						</tr>
						<tr>
							<td><label for=\"from_date\">Dal: </label></td>
							<td><input type=\"text\" name=\"from_date\" id=\"from_date\" value=\"$fromDate\" />
								" . selectDate("searchDocument", "from_date") . "
							</td>
						</tr>
						<tr>
							<td><label for=\"to_date\">Al: </label></td>
							<td><input type=\"text\" name=\"to_date\" id=\"to_date\" value=\"$toDate\" />
								" . selectDate("searchDocument", "to_date") . "
							</td>
        				</tr>
        				<tr>
							<td><label for=\"expires\">Solo documenti validi ad oggi: </label></td>
							<td><input type=\"checkbox\" name=\"expires\" id=\"expires\" $expires /></td>
        				</tr>
        				<tr>
							<td><label for=\"type\">Tipologia di documento: </label></td>
							<td>
								<select name=\"type\" id=\"type\">
									<option value =\"Tutti\" selected=\"selected\">Tutti</option>
									" . showOptions("type", "document_types", $type) . "
								</select>
							</td>
						</tr>
						$departmentHtml
						<tr>
							<td><label for=\"description\">Descrizione: </td>
							<td><textarea rows=\"4\" cols=\"20\" name=\"description\" id=\"description\">$description</textarea></td>
						</tr>
					</table>
					<input name=\"Ricerca\" type=\"submit\" value=\"Ricerca\" />
				</form>";
	} 



	/*
	* function selectDate()
	*
	* Input required:   $formName: the name of the form
	*                   $fieldName: the name of the input containg the js
	*
	* Output:   return a js to select the date
	*
	* Author: Lisa Pedrazzi
	*/

	function selectDate($formName, $fieldName) {
		return "
								<a href=\"javascript:show_calendar('document.$formName.$fieldName', document.$formName.$fieldName.value);\">
									<img src=\"./img/style/calendar.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Seleziona la data\">
								</a>";
	}



	/*
	* function normal2MysqlDate()
	*
	* Input required:	$date: string with the date to convert
	* Output:	return a string with the date format converted from DD/MM/YYYY to YYYY-MM-DD
	*
	* Author: Federico Zanco
	*/

	function normal2MysqlDate($date) {
			$date = explode("/",$date);
			return $date[2] . "-" . $date[1] . "-" . $date[0];
	}



	/*
	* function mysql2NormalDate()
	*
	* Input required:	$date: string with the date to convert
	*
	* Output:	return a string with the date format converted from YYYY-MM-DD to DD/MM/YYYY 
	*			or the string N/A if $date is 0000-00-00 (Null date)
	*
	* Author: Federico Zanco
	*/

	function mysql2NormalDate($date) {
		if ($date != "0000-00-00") {
			$date = explode("-",$date);
			return $date[2] . "/" . $date[1] . "/" . $date[0];
		} else
			return "N/A";
	}

?>